"""
_*_ : utf-8 _*_ 
@Time: 2023/11/11 17:19
@Author: LWK
@File: 071_获取百度一下四个字
@Project: demo01
"""
# （1）获取网页源码
# （2）解析服务器响应的文件 etree.HTML
# （3）打印结果

import urllib.request
from lxml import etree


url = 'https://www.baidu.com'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76'
}

req = urllib.request.Request(url=url, headers=headers)
res = urllib.request.urlopen(req)
# 源码
content = res.read().decode('utf-8')
# 解析服务器响应文件
tree = etree.HTML(content)
# 获取想要的数据
res = tree.xpath('//input[@id="su"]/@value')
print(res)
